package com.hmy.finance.controller.receivable;


import cn.hutool.core.collection.CollectionUtil;
import com.hmy.ccp.common.response.BasePageResponse;
import com.hmy.ccp.common.result.JsonResult;
import com.hmy.ccp.error.annotations.HmyGateWayController;
import com.hmy.finance.bo.request.ReceivablesQueryRequest;
import com.hmy.finance.bo.response.ReceivablesExportResponse;
import com.hmy.finance.bo.response.ReceivablesQueryResponse;
import com.hmy.finance.service.core.IReceivableInventoryService;
import com.hmy.finance.service.core.dto.OverdueDetailDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "应收管理")
@RestController
@RequestMapping("/api/receivable")
@Validated
@Slf4j
@HmyGateWayController
public class ReceivableController {

    @Resource
    private IReceivableInventoryService iReceivableInventoryService;

    /**
     * 分页查询应收盘点
     */
    @ApiOperation("分页查询应收盘点")
    @PostMapping("/queryReceivablesByPage")
    public JsonResult<BasePageResponse<ReceivablesQueryResponse>> queryReceivablesByPage(@RequestBody @Validated ReceivablesQueryRequest receivablesQueryRequest) {
        BasePageResponse<ReceivablesQueryResponse> response = iReceivableInventoryService.queryReceivablesByPage(receivablesQueryRequest);
        //为前端补足逾期list，挺定制的搞法
        if (CollectionUtil.isNotEmpty(response.getRecords())) {
            response.getRecords().forEach(receivablesQueryResponse -> {
                if (CollectionUtil.isEmpty(receivablesQueryResponse.getOverdueDetail())) {
                    List<OverdueDetailDto> overdueDetail = new ArrayList<>();
                    OverdueDetailDto overdueDetail30 = new OverdueDetailDto();
                    overdueDetail30.setSort(1);
                    overdueDetail30.setOverdueDaysStart(1);
                    overdueDetail30.setOverdueDaysEnd(30);
                    overdueDetail30.setOverdueDaysDesc("逾期" + 1 + "-" + 30 + "天内金额 (元)");
                    overdueDetail30.setOverdueAmount(0.00);
                    overdueDetail.add(overdueDetail30);
                    OverdueDetailDto overdueDetail60 = new OverdueDetailDto();
                    overdueDetail60.setSort(2);
                    overdueDetail60.setOverdueDaysStart(31);
                    overdueDetail60.setOverdueDaysEnd(60);
                    overdueDetail60.setOverdueDaysDesc("逾期" + 31 + "-" + 60 + "天内金额 (元)");
                    overdueDetail60.setOverdueAmount(0.00);
                    overdueDetail.add(overdueDetail60);
                    OverdueDetailDto overdueDetail90 = new OverdueDetailDto();
                    overdueDetail90.setSort(3);
                    overdueDetail90.setOverdueDaysStart(61);
                    overdueDetail90.setOverdueDaysEnd(90);
                    overdueDetail90.setOverdueDaysDesc("逾期" + 61 + "-" + 90 + "天内金额 (元)");
                    overdueDetail90.setOverdueAmount(0.00);
                    overdueDetail.add(overdueDetail90);
                    OverdueDetailDto overdueDetail91 = new OverdueDetailDto();
                    overdueDetail91.setSort(4);
                    overdueDetail91.setOverdueDaysStart(91);
                    overdueDetail91.setOverdueDaysEnd(9999);
                    overdueDetail91.setOverdueDaysDesc("逾期" + 91 + "天" + "以上金额 (元)");
                    overdueDetail91.setOverdueAmount(0.00);
                    overdueDetail.add(overdueDetail91);
                    receivablesQueryResponse.setOverdueDetail(overdueDetail);
                }
            });
        }


        return JsonResult.success(response);
    }

    /**
     * 应收盘点导出
     */
    @ApiOperation("应收盘点导出")
    @PostMapping("/exportReceivablesByParams")
    public JsonResult<ReceivablesExportResponse> exportReceivablesByParams(@RequestBody @Validated ReceivablesQueryRequest receivablesExportRequest) {
        ReceivablesExportResponse response = iReceivableInventoryService.exportReceivablesByParams(receivablesExportRequest);
        return JsonResult.success(response);
    }


}
